﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using ZLCMS.Data.Model;
using NHibernate.Criterion;

namespace ZLCMS.Web.SystemManage.Basics.MealOrder
{
    public partial class OrderProcess : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected string _userAccount;
        protected string _owner;

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();

            //初始化DWZ控件
            _framePage = new FrameHelper(this);
            _owner = UserKit.GetUserAccountFailLogin();
            //判断是哪种操作
            if (Request["ajax"] == "dels")
            {
                string ids = Request["ids"];
                if (!string.IsNullOrEmpty(ids))
                {
                    string[] idStrs = ids.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    IList<int> idList = new List<int>();
                    foreach (string acc in idStrs)
                    {
                        if (!string.IsNullOrEmpty(acc))
                        {
                            idList.Add(Int32.Parse(acc));
                        }

                    }

                    #region 根据ID,删除订单及相应的订单商品记录
                    //根据ID,删除订单及相应的订单商品记录
                    using (ISession session = SessionFactoryHelper.CreateSession())
                    {
                        IList<ShopOrderInfo> orderList = session.CreateCriteria<ShopOrderInfo>().Add(Expression.Eq("OwnerAccount", _owner)).Add(Expression.In("Id", idList.ToArray())).List<ShopOrderInfo>();
                        foreach (ShopOrderInfo order in orderList)
                        {
                            IList<ShopOrderProduct> productList = session.CreateCriteria<ShopOrderProduct>().Add(Expression.Eq("OwnerAccount", _owner)).Add(Expression.Eq("OrderNo", order.OrderNo)).List<ShopOrderProduct>();
                            foreach (ShopOrderProduct product in productList)
                            {
                                session.Delete(product);
                            }
                            session.Delete(order);
                        }
                        session.Flush();
                        _framePage.Alert(new ReturnCode() { statusCode = 200, message = "操作成功!", navTabId = _framePage.NavTabId });
                    } 
                    #endregion
                }
                else
                {
                    _framePage.Alert(new ReturnCode() { statusCode = 300, message = "参数错误" });
                }
            }
        }
    }
}